package priv.dajie.tzzq.spider.csgo.steam;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.HttpGet;
import priv.dajie.tzzq.Base;
import priv.dajie.tzzq.spider.csgo.SpiderInfoElement;
import priv.dajie.tzzq.spider.csgo.SpiderInfos;
import priv.dajie.tzzq.spider.model.SpiderInfo;
import sun.net.www.http.HttpClient;

import java.util.*;

/**
 * @ClassName SteamCsgoInfo
 * @Description
 * @Author dajie
 * @Date 2019/11/9 1:01 上午
 * @Version V1.0
 **/
public class SteamCsgoInfo {

    private static Base httpclient = Base.getClientInstance();
    private static List<String> eliminates;

    static {
        eliminates = Arrays.asList("CSGO_Type_Pistol", "CSGO_Type_Knife", "CSGO_Type_Machinegun",
                "CSGO_Type_Rifle", "CSGO_Type_SMG", "CSGO_Type_Shotgu", "CSGO_Type_SniperRifle");
    }


    public static SpiderInfos getSpiderInfos(){
        String jsonStr = httpclient.open(new HttpGet("https://steamcommunity.com/market/appfilters/730"), true);
        JSONObject json = JSON.parseObject(jsonStr);
        JSONObject facets = json.getJSONObject("facets");
        JSONObject weapon = facets.getJSONObject("730_Weapon").getJSONObject("tags");
        Map<SpiderInfoElement, String> elementStringMap = new HashMap<>();
        List<SpiderInfoElement> elementList = new ArrayList<>();
        for (Map.Entry<String, Object> item : weapon.entrySet()) {
            SpiderInfoElement spiderInfoElement = new SpiderInfoElement();
            spiderInfoElement.setKey("category_730_Weapon[]");
            String cn = ((JSONObject) item.getValue()).getString("localized_name");
            spiderInfoElement.setValue("tag_"+item.getKey());
            spiderInfoElement.setTypeName(cn);
            elementList.add(spiderInfoElement);
        }
        JSONObject type = facets.getJSONObject("730_Type").getJSONObject("tags");
        for (Map.Entry<String, Object> item : type.entrySet()) {
            String value = item.getKey();
            if (eliminates.contains(value)){
                continue;
            }
            SpiderInfoElement spiderInfoElement = new SpiderInfoElement();
            spiderInfoElement.setKey("category_730_Type[]");
            String cn = ((JSONObject) item.getValue()).getString("localized_name");
            spiderInfoElement.setValue("tag_"+value);
            spiderInfoElement.setTypeName(cn);
//            elementStringMap.put(spiderInfoElement, cn);
            elementList.add(spiderInfoElement);
        }
        return new SpiderInfos(elementList);
    }

    public static void main(String[] args) {
        SpiderInfos spiderInfos = getSpiderInfos();
        System.out.println(spiderInfos);
    }
}
